<?php namespace models;
/**
 * This code is property of Piotr Poradzisz. Do not distribute under threat of legal consequences.
 * All rights reserved.
 * Все права защищены.
 * Alle Rechte vorbehalten.
 * Todos los derechos reservados.
 * Wszelkie prawa zastrzeżone.
 *                  جميع الحقوق محفوظة
 * 保留所有权利
 * すべての著作権
 * @author Piotr Poradzisz
 * email: piotr.poradzisz@gmail.com
 * tel.: +48668027020
 */
class Field{

    public static function getFieldProperties($id, $columns = null){
        $mysql = new Mysql();
        $select = ($columns ? $columns : 'f.*');
        $query = "  SELECT  $select,
                            fta.id as 'fieldTag_id', fta.name as 'fieldTag',
                            fty.id as 'fieldType_id', fty.name as 'fieldType',
                            fr.id as 'formRights_id' , fr.name as 'formRights'
                    FROM    field f JOIN (formRights fr, fieldTag fta, fieldType fty)
                    ON      (f.formRights = fr.id AND f.fieldTag = fta.id AND f.fieldType = fty.id)
                    WHERE   f.id = $id  ";
        return $mysql->getRow($query);
    }

    protected static function getFields($menu, $condition, $itemId = null){
        $mysql = new Mysql();
        if($itemId){
            $select = 'fd.list as listDisplay, fd.details as detailsDisplay, ';
            $join = "  LEFT JOIN fieldDisplay fd ON (f.id = fd.childe AND fd.parent = $itemId)";
            $position = 'fd.position';
        }
        else $position = 'f.position';
        $query = "  SELECT  f.id as 'fieldId', f.title, f.tableName, f.name, f.referenceTable, f.javascript, f.value, f.accept, f.accesskey, f.alt, f.class, f.disabled, f.multiple, f.readonly, f.maxlength, f.size, f.params, f.control, f.relation, $select
                            fta.id as 'fieldTag_id', fta.name as 'fieldTag',
                            fty.id as 'fieldType_id', fty.name as 'fieldType',
                            fr.id as 'formRights_id' , fr.name as 'formRights'
                    FROM field f JOIN (formRights fr, tableRights tr, fieldTag fta, fieldType fty)
                    ON (f.formRights = fr.id AND f.tableRights = tr.id AND f.fieldTag = fta.id AND f.fieldType = fty.id) $join
                    WHERE f.menu = $menu AND $condition = 1 AND f.active = 1
                    ORDER BY $position ASC   ";
        return $mysql->getTable($query);
    }

    protected function setFieldProperties($id, $propertiesArray){
        $mysql = new Mysql();
        $select = implode(",", $propertiesArray);
        $query = "  SELECT $select FROM field WHERE id = $id";
        foreach($mysql->getRow($query) as $propertyKey => $value){
            if($propertyKey == 'params'){
                $params = explode(';', $value);
                foreach($params as $paramKey => $param){
                    $param = explode('=',$param);
                    if(stristr($param[1], '[') == true) {
                        $values = explode('[', $param[1]);                        
                        $values = explode(']',$values[1]);
                        $values = explode(',',$values[0]);
                        $this->{$param[0]} = array();
                        foreach($values as $arrayKey => $array){
                            
                            $value = explode('>', $array);
                            $this->{$param[0]}[$value[0]] = $value[1];
                        }
                    }
                    else $this->{$param[0]} = $param[1];
                }
            }
            else $this->{$propertyKey} = $value;
        }
    }

    protected function setSequence($tableName, $sequence) {
        $mysql = new Mysql();
        foreach($sequence as $position => $item){
            $query = "UPDATE $tableName SET position = $position WHERE id = $item";
            $mysql->setQuery($query);
        }
    }

     public static function setDisplay($type, $newStatus, $fieldId, $itemId){
        $mysql = new Mysql();
        $query = "SELECT id, list, details FROM fieldDisplay WHERE parent = $itemId AND childe = $fieldId";
        $currantStatus = $mysql->getRow($query);
        if($currantStatus){
            if($newStatus == 0){                    
                if(($type == 'list' and ($currantStatus['details'] == 0 OR !$currantStatus['details'])) OR ($type == 'details' and ($currantStatus['list'] == 0 OR !$currantStatus['list']))){
                    $query = "DELETE FROM fieldDisplay WHERE id = $currantStatus[id]";
                    $delete = true;
                }
                else  $query = "UPDATE fieldDisplay SET $type = 0  WHERE id = $currantStatus[id]";
            }
            else $query = "UPDATE fieldDisplay SET $type = 1  WHERE id = $currantStatus[id]";
        }
        else{
            $query = "INSERT INTO fieldDisplay (parent, childe, $type) VALUES ($itemId, $fieldId, $newStatus)";
        }
        $mysql->setQuery($query);
        if($delete === true) return 0;
        else return 1;
    }    

    public static function setFieldSequence() {
        $mysql = new Mysql();
        foreach($_POST['sequence'] as $position => $item){
            $query = "UPDATE fieldDisplay SET position = $position WHERE childe = $item AND parent = $_POST[itemId]";
            $mysql->setQuery($query);
        }
    }

}
?>